"""
A class to exercise the JT core functions.
"""
import itk
import vtk

import connect2
import ImageCompare

import JT
import Calibration

def Test():
    """
    Test image difference function updated from DRR.
    An observer is added to look for the keypress 'u' to update the differnce image.
    """

    inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
    staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
    calFileLat = 'C:/Users/bryan/bryan-code/trunk/TestData/ext_cal1.txt'

    #fixedImageName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mhd'
    fixedImageName = 'C:/Users/bryan/bryan-code/trunk/Images/ucharim080-LAT.png'
    projectedImageName = 'C:/Users/bryan/bryan-code/trunk/Images/CurrentDRR.mhd'
    outPath = 'C:/Users/bryan/bryan-code/trunk/Tests/'

#===============================================================================
#
#    Fixed Image Components
#
#===============================================================================
    itk_reader = itk.ImageFileReader.IF2.New()
    ic = JT.ImageCanvas()
    regDisp = JT.ImageCanvas()

    textActor = vtk.vtkTextActor()
    textActor.SetInput("null")
    tprop = textActor.GetTextProperty()
    tprop.SetColor(1,0,0)

#===============================================================================
#
#    Generate Moving Image Projection
#
#===============================================================================
    xraycam = JT.XRayCamera()
    drr = JT.DRR()
    drr.SetXRayCamera(xraycam)
    drr.SetVolumeFilename(inputVolFileName)
    drr.SetBlackOnWhiteImage()


#===============================================================================
#
#    Automatic calibration
#
#===============================================================================
    cal = Calibration.ExternalCalibration()
    cal.LoadConsolidatedCalibration(calFileLat)
    cal.LoadStaFile(staFile)

    xraycam.SetExternalCalibration(cal)

    vtkMatrix = cal.ConvertNumpyToVTKArray(cal._VolumeT)
    drr.SetVolumeUserMatrix(vtkMatrix)

#===============================================================================
#
#    Open Fixed Image
#
#===============================================================================
    itk_reader.SetFileName(fixedImageName)
    itk_reader.Update()

    ic.SetImage(itk_reader.GetOutput())

#===============================================================================
#
#    Set up the registration
#
#===============================================================================
    reg = JT.NewRegistration()
    reg.SetMovingImage(drr.GetImage('itk'))
    reg.SetFixedImage(itk_reader.GetOutput())

#===============================================================================
#
#    Update Callback Function
#
#===============================================================================
    def Update(obj,event):
        keysym = drr._iren.GetKeySym()
        #if keysym == 'g':
        #print "Generating Metric Value"
        reg.SetMovingImage(drr.GetImage('itk'))
        value = reg.GetMetric()
        textActor.SetInput(str(value))
        drr._renWin.Render()
        ic.Render()

    drr._iren.AddObserver("KeyPressEvent",Update)

    ic._ren.AddActor2D(textActor)

    drr._renWin.SetSize(512,512)

    #UpdateDiffImageWindow()
    ic.Render()
    drr.Render()

    print "Writing image to disk..."
    outPath = 'C:/Users/bryan/bryan-code/trunk/Images/RegOut.mhd'
    regDisp.CheckerImages(itk_reader.GetOutput(),drr.GetImage('itk'),outPath)

    itk.echo(itk_reader.GetOutput())
    itk.echo(drr.GetImage('itk'))




if __name__ == "__main__":
    Test()